﻿#include "api_user.h"

User::User(QObject *parent) : QObject(parent)
{

}

// 发送登录请求
void User::user_login(QJsonObject &data) {
    QString url = "/user/login/";
    QString jwt_token = "";
    this->request = new HttpPostRequest();
    this->request->post(url, jwt_token, data);

    QObject::connect(this->request, &HttpPostRequest::post_finished, this, &User::user_login_finished);
}

// 登录请求发送完成后
void User::user_login_finished() {
    emit this->user_login_after();
    QObject::disconnect(this->request, &HttpPostRequest::post_finished, this, &User::user_login_finished);
}

// 发送注册请求
void User::user_register(QJsonObject &data) {
    QString url = "/user/register/";
    QString jwt_token = "";
    this->request = new HttpPostRequest();
    this->request->post(url, jwt_token, data);

    QObject::connect(this->request, &HttpPostRequest::post_finished, this, &User::user_register_finished);
}

// 注册请求发送完成后
void User::user_register_finished() {
    emit this->user_register_after();
    QObject::disconnect(this->request, &HttpPostRequest::post_finished, this, &User::user_register_finished);
}

// 发送获取当前用户信息的请求
void User::user_get_cur_user_info() {
    QString url = "/user/get/current/user/info/";
    QString jwt_token = this->get_jwt_token();
    QJsonObject data;
    this->request = new HttpPostRequest();
    this->request->post(url, jwt_token, data);

    QObject::connect(this->request, &HttpPostRequest::post_finished, this, &User::user_get_cur_user_info_finished);
}

// 发送获取当前用户信息的请求完成后
void User::user_get_cur_user_info_finished() {
    QJsonObject tmp = this->request->get_return_data().toObject();

    this->username = tmp["username"].toString();
    this->id_kind = tmp["idKind"].toInt();
    if (this->id_kind == 0) {
        this->id_kind_string = "顾客";
    } else if (this->id_kind == 1) {
        this->id_kind_string = "服务员";
    } else if (this->id_kind == 2) {
        this->id_kind_string = "厨师";
    } else if (this->id_kind == 3) {
        this->id_kind_string = "管理员";
    } else {
        this->id_kind_string = "未知";
    }
    this->id = tmp["id"].toInt();

    emit this->user_get_cur_user_info_after();
    QObject::disconnect(this->request, &HttpPostRequest::post_finished, this, &User::user_get_cur_user_info_finished);
}

// 退出登录
void User::user_logout() {
    this->set_jwt_token("");
    this->username = "";
    this->id_kind = -1;
    this->id_kind_string = "";
}

void User::get_detail_info() {

}
